<?php
/**
* DPMatchReserve copyright (c) 2008 sk89q
* @ http://digitalpaint.therisenrealm.com
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*/

require_once "./functions.php";
require_once "include/DigitalPaintServer.php";

// ===============================
// Get input
// ===============================

$server = $_GET['server'];

$server_info = @$CONFIG['servers'][$server];

if(!isset($CONFIG['servers'][$server]))
{
    echo "No server specified.";
    exit();
}

// ===============================
// Get status
// ===============================

try
{
    $paintball = new DigitalPaintServer($server_info['host'], $server_info['port'], $server_info['rcon_password']);

    if(!$paintball->is_online())
    {
        $status = "offline";
    }
    else if(count($paintball->get_players()) == 0)
    {
        $status = "available";
        
        if($CONFIG['grace_period'] > 0)
        {
            $last_lease = @filemtime("cache/last_lease_$server");
            
            if($last_lease > time()-$CONFIG['grace_period'])
            {
                $status = "reserved";
            }
        }
    }
    else
    {
        $status = "reserved";
    }
}
catch(DPSConnectionException $e)
{
    $status = "offline";
}

// ===============================
// Send it off!
// ===============================

header("Content-Type: image/png");
readfile("themes/{$CONFIG['theme']}/images/status/{$status}.png");
?>